package com.teatrace.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.teatrace.model.base.Customer;
import com.teatrace.model.base.Dealer;
import com.teatrace.model.base.Employee;
import com.teatrace.model.base.Vehicle;
import com.teatrace.model.pack.Goods;
import com.teatrace.model.pack.PackingFlow;
import com.teatrace.model.sell.Delivery;
import com.teatrace.model.sell.Order;
import com.teatrace.model.sell.OrderItem;
import com.teatrace.service.ServiceContext;
import com.teatrace.service.basic.BasicService;
import com.teatrace.service.sell.SellService;

public class SellAction extends ActionSupport implements RequestAware , SessionAware{


	/**
	 * 
	 */
	private static final long serialVersionUID = -8557375948794272609L;
	private static final Logger log = Logger.getLogger(BasicService.class); 
	private Map<String , Object> req;
	private Map<String , Object> session;
	private BasicService service;
	private SellService sellService;
	private Order order;
	private OrderItem orderItem;
	private Delivery delivery;
	private Dealer dealer;
	private Goods goods;
	private Customer customer;
	private PackingFlow packingFlow;
	private Vehicle vehicle;
	private Employee emp;
	/**
	 * name 接收addOrder的多个明细的商品名称
	 * goodsSpecifications 接收 addOrder的多个明细的商品包装类型
	 * numbers 接收每个明细的商品数量
	 */
	private String[] names;
	private String[] types;
	private int[] nums;
	/**
	 * 在发货时，商品的编号
	 */
	private String goodNumbers;
	private String udName;
	private String drName;
	
	
	public SellAction() {
		service = ServiceContext.getService(BasicService.class);
		sellService = ServiceContext.getService(SellService.class);
	}
	/**
	 * 以上为变量的定义
	 */
	public String ordersList() throws Exception {
		System.out.println("enter ordersList ……");
		listOrders();
		return "ordersList";
	}
	
	private void listOrders() throws Exception {
		List<Order> deliList = new ArrayList<Order>();
		List<Order> noList = new ArrayList<Order>();
		try {
			List<Order> list = new ArrayList<Order>();
			list = sellService.listObject(Order.class);
			for(Order ord : list) {
				if(0 != ord.getStatus()) {
					deliList.add(ord);
				} else {
					noList.add(ord);
				}
			}
			System.out.println("已发订单 : " + deliList.size());
			System.out.println("待发订单 : " + noList.size());
		}catch (Exception ex) {
			log.error(ex,ex);
		}
		req.put("deliList", deliList);
		req.put("noList", noList);
	}
	
	public String orderToAdd() throws Exception {
		System.out.println("enter orderToAdd!");
		List<String> nameList = new ArrayList<String>();
		List<String> spfList = new ArrayList<String>();
		try {
			List<PackingFlow> list = sellService.listObject(PackingFlow.class);
			for(PackingFlow	pf : list) {
				nameList.add(pf.getGoodsName());
				spfList.add(pf.getGoodsSpecification());
			}
		}catch (Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		System.out.println("商品名数量："+nameList.size());
		System.out.println("商品名规格："+spfList.size());
		req.put("nameList", nameList);
		req.put("spfList", spfList);
		return "orderToAdd";
	}
	
	public String orderAdd() throws Exception {
		System.out.println("enter ooderAdd ……");
		try {
			Date time = order.getTime();
			List<Dealer> dList = service.listByCriteria(Dealer.class, "name", dealer.getName());
			dealer = dList.get(0);
			order.setDealer(dealer);
			order.setStatus(0);
			service.add(order);
			List<Order> oList = service.listByCriteria(Order.class, "time", time);
			order = oList.get(0);
			OrderItem oitem = new OrderItem();
			System.out.println("order: " + order.getId());
			for(int i = 0; i < names.length; i++) {
				oitem.setOrder(order);
				oitem.setGoodsName(names[i]);
				oitem.setGoodsSpecification(types[i]);
				oitem.setNumber(nums[i]);
				service.add(oitem);
			}
			listOrders();
		}catch(Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		return "orderAdd";
	}
	
	public String listOrderDesc() throws Exception {
		System.out.println("enter ListOrderDesc! ");
		List<OrderItem> odItem = new ArrayList<OrderItem>();
		try {
			order = service.getById(Order.class, order.getId());
			System.out.println("Order: " + order.getId());
			odItem = service.listByCriteria(OrderItem.class, "order", order);
		}catch(Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		req.put("order", order);
		req.put("orderItem", odItem);
		return "listOrderDesc";
	}
	
	public String orderUpdate() throws Exception {
		System.out.println("enter orderUpdate!");
		try {
			List<Dealer> dList = service.listByCriteria(Dealer.class, "name", dealer.getName());
			dealer = dList.get(0);
			order.setDealer(dealer);
			service.update(order);
			long id = order.getId();
			List<Order> odList = service.listByCriteria(Order.class, "id", id);
			order = odList.get(0);
			List<OrderItem> odItem = service.listByCriteria(OrderItem.class, "order", order);
			for(OrderItem item : odItem) {
				long iId = item.getId();
				service.deleteById(OrderItem.class, iId);
			}
			OrderItem oitem = new OrderItem();
			for(int i = 0; i < names.length; i++) {
				oitem.setOrder(order);
				oitem.setGoodsName(names[i]);
				oitem.setGoodsSpecification(types[i]);
				oitem.setNumber(nums[i]);
				service.add(oitem);
			}
			listOrders();
		}catch (Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		return "orderUpdate";
	}
	
	public String delivery() throws Exception {
		System.out.println("enter delivery");
		List<OrderItem> odItem = new ArrayList<OrderItem>();
		try {
			order = service.getById(Order.class, order.getId());
			System.out.println("Order: " + order.getId());
			odItem = service.listByCriteria(OrderItem.class, "order", order);
		}catch(Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		req.put("order", order);
		req.put("orderItem", odItem);
		return "delivery";
	}
	
	@SuppressWarnings("unchecked")
	public void deliverying() throws Exception {
		System.out.println("enter deliverying");
		try {
			List list = new ArrayList();
			List liItem = new ArrayList();
			System.out.println("orderItemId : " + orderItem.getId());
			System.out.println("goodNumbers : " + goodNumbers);
			liItem.add(orderItem.getId());
			liItem.add(goodNumbers);
			if(session.get("orderItemTodeliverylist") == null) {
				list.add(liItem);
			}
			else {
				list = (List) session.get("orderItemTodeliverylist");
				System.out.println("+++++++++");
				list.add(liItem);
			}
			session.put("orderItemTodeliverylist", list);
		}catch (Exception ex) {
			log.error(ex,ex);
		}
	}
	
	@SuppressWarnings("unchecked")
	public String deliveryProduct() throws Exception {
		System.out.println("enter deliveryProduct!");
		List list = new ArrayList();
		try {
			list = (List) session.get("orderItemTodeliverylist");
			List iList = new ArrayList();
			for(int i = 0 ; i < list.size(); i++) {
				iList = (List) list.get(i);
				System.out.println("订单明细号："+iList.get(0) + " 包含的商品编号有：" + iList.get(1));
			}
			session.remove("orderItemTodeliverylist");
			System.out.println("vehicleName : " + vehicle.getName());
			System.out.println("orderId : " + order.getId());
			order = service.getById(Order.class, order.getId());
			order.setStatus(1);
			service.update(order);
		}catch (Exception ex) {
			session.remove("orderItemTodeliverylist");
			log.error(ex,ex);
			return "FAIL";
		}
		return "SUCCESS";
	}
	
	
	public String listPurchase() throws Exception {
		System.out.println("enter purchase");
		List<Goods> listGoods = new ArrayList<Goods>();
		try {
			listGoods = sellService.listObject(Goods.class);
		}catch(Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		req.put("purchase", listGoods);
		return "listPurchase";
	}
	
	public String addPurchase() throws Exception {
		System.out.println("enter addPurchase");
		List<Goods> listGoods = new ArrayList<Goods>();
		try {
			List<Customer> cList = service.listByCriteria(Customer.class, "name", customer.getName());
			customer = cList.get(0);
			goods.setCustomer(customer);
			service.add(goods);
			listGoods = sellService.listObject(Goods.class);
		}catch(Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		req.put("purchase", listGoods);
		return "listPurchase";
	}
	
	public String toUpdatePurchase() throws Exception {
		System.out.println("enter toUpdatePurchase");
		try {
			goods = service.getById(Goods.class, goods.getGoodsId());
		}catch(Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		req.put("good", goods);
		return "toUpdatePurchase";
	}
	
	public String updatePurchase() throws Exception {
		System.out.println("enter updatePurchase");
		List<Goods> listGoods = new ArrayList<Goods>();
		try {
			List<Customer> cList = service.listByCriteria(Customer.class, "name", customer.getName());
			customer = cList.get(0);
			goods.setCustomer(customer);
			service.update(goods);
			listGoods = sellService.listObject(Goods.class);
		}catch (Exception ex) {
			log.error(ex,ex);
			return "FAIL";
		}
		req.put("purchase", listGoods);
		return "updatePurchase";
	}
	
	/**
	 * 以下为set;get;方法	
	 */
	public void setRequest(Map<String, Object> req) {
		// TODO Auto-generated method stub
		this.req = req;
	}
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	public OrderItem getOrderItem() {
		return orderItem;
	}
	public void setOrderItem(OrderItem orderItem) {
		this.orderItem = orderItem;
	}
	public Delivery getDelivery() {
		return delivery;
	}
	public void setDelivery(Delivery delivery) {
		this.delivery = delivery;
	}
	public Dealer getDealer() {
		return dealer;
	}
	public void setDealer(Dealer dealer) {
		this.dealer = dealer;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public PackingFlow getPackingFlow() {
		return packingFlow;
	}
	public void setPackingFlow(PackingFlow packingFlow) {
		this.packingFlow = packingFlow;
	}
	public String[] getNames() {
		return names;
	}
	public void setNames(String[] names) {
		this.names = names;
	}
	public String[] getTypes() {
		return types;
	}
	public void setTypes(String[] types) {
		this.types = types;
	}
	public int[] getNums() {
		return nums;
	}
	public void setNums(int[] nums) {
		this.nums = nums;
	}
	public String getGoodNumbers() {
		return goodNumbers;
	}
	public void setGoodNumbers(String goodNumbers) {
		this.goodNumbers = goodNumbers;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setVehicle(Vehicle vehicle) {
		this.vehicle = vehicle;
	}
	public Employee getEmp() {
		return emp;
	}
	public void setEmp(Employee emp) {
		this.emp = emp;
	}
	public String getUdName() {
		return udName;
	}
	public void setUdName(String udName) {
		this.udName = udName;
	}
	public String getDrName() {
		return drName;
	}
	public void setDrName(String drName) {
		this.drName = drName;
	}
}
